package com.android.hidephotosplus.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import com.android.hidephotosplus.AppSettings;
import com.android.hidephotosplus.R;
import com.android.hidephotosplus.db.DbHelper;
import com.android.hidephotosplus.db.DbProvider;
import com.android.hidephotosplus.interfaces.MInterface.onTask;
import com.android.hidephotosplus.model.ModelAlbum;
import com.android.hidephotosplus.model.ModelMedia;
import com.android.hidephotosplus.preference.StringPref;
import com.android.hidephotosplus.service.HidePhotoService;

public class Util {

	public static boolean copyFile(String from, String to) {
		try {
			int bytesum = 0;
			int byteread = 0;
			File oldfile = new File(from);
			if (oldfile.exists()) {
				InputStream inStream = new FileInputStream(from);
				FileOutputStream fs = new FileOutputStream(to);
				byte[] buffer = new byte[from.getBytes().length];
				while ((byteread = inStream.read(buffer)) != -1) {
					bytesum += byteread;
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
				fs.close();
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public static class HiddenMedia extends AsyncTask<Void, Integer, Void> {

		private Activity activity;

		private ArrayList<ModelMedia> arrMedias;

		private ArrayList<ModelAlbum> arrAlbums;

		private String pathAlbum = null;

		private DbHelper mDbHelper;

		private DbProvider mDbProvider;

		private long idAlbum;

		private ProgressDialog progressDialog;

		private onTask mTask;

		private int mProgressStatus = 0;

		private int sumMedia = 0;

		private String nameAlbum;

		private File filePathAlbum = null;

		private String navigationSelected;

		private boolean showProgress;

		int typeStorage;

		public HiddenMedia(Activity activity, ArrayList<ModelMedia> arrMedias,
				ArrayList<ModelAlbum> arrAlbums, String navigationSelected,
				boolean showProgress, onTask mTask) {
			this.activity = activity;
			this.arrMedias = arrMedias;
			mDbHelper = DbHelper.getInstance(activity);
			mDbProvider = DbProvider.getinstance(activity);
			this.mTask = mTask;
			this.arrAlbums = arrAlbums;
			this.navigationSelected = navigationSelected;
			this.showProgress = showProgress;
			typeStorage = Integer.parseInt(StringPref.getPreference(activity,
					Config.KEY_STORAGE, "1"));
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			if (arrAlbums != null) {
				if (navigationSelected.equals(activity
						.getString(R.string.all_media))) {
					for (ModelAlbum album : arrAlbums) {
						ArrayList<ModelMedia> arrMedias = mDbProvider
								.getAllMedias(album.getIdAlbum());
						sumMedia += arrMedias.size();
					}
				} else if (navigationSelected.equals(activity
						.getString(R.string.photos))) {
					for (ModelAlbum album : arrAlbums) {
						ArrayList<ModelMedia> arrMedias = mDbProvider
								.getAllPhotos(album.getIdAlbum());
						sumMedia += arrMedias.size();
					}
				} else {
					for (ModelAlbum album : arrAlbums) {
						ArrayList<ModelMedia> arrMedias = mDbProvider
								.getAllVideos(album.getIdAlbum());
						sumMedia += arrMedias.size();
					}
				}
			} else
				sumMedia = arrMedias.size();

			if (showProgress) {
				progressDialog = new ProgressDialog(activity);
				progressDialog.setMax(sumMedia);
				progressDialog.setMessage(activity
						.getString(R.string.hiding_media));
				progressDialog
						.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
				progressDialog.setProgressDrawable(activity.getResources()
						.getDrawable(R.drawable.custom_progress));
				progressDialog.setCancelable(true);
				progressDialog.setProgress(0);
				progressDialog.show();
			}

			// ============Setup Hide Media
			if (arrMedias != null && arrMedias.size() > 0) {
				nameAlbum = arrMedias.get(0).getNameAlbum();
				filePathAlbum = new File(Config.getPathFinal(typeStorage)
						+ nameAlbum);

				pathAlbum = filePathAlbum.getAbsolutePath();
				if (!filePathAlbum.exists()) {
					if (filePathAlbum.mkdirs())
						idAlbum = mDbHelper.insertAlbum(nameAlbum, pathAlbum,
								typeStorage);
				} else {
					if (!mDbHelper.checkExistsAlbum(nameAlbum)) {
						idAlbum = mDbHelper.insertAlbum(nameAlbum, pathAlbum,
								typeStorage);
					} else
						idAlbum = mDbHelper.getIdAlbumByNameAlbum(nameAlbum);
				}
			}
		}

		@Override
		protected Void doInBackground(Void... params) {
			while (mProgressStatus < sumMedia) {
				if (arrAlbums != null) {
					for (ModelAlbum album : arrAlbums) {
						nameAlbum = album.getNameAlbum();
						filePathAlbum = new File(
								Config.getPathFinal(typeStorage)
										+ album.getNameAlbum());
						pathAlbum = filePathAlbum.getAbsolutePath();
						Log.e("pathAlbum", pathAlbum);
						if (!filePathAlbum.exists()) {
							if (filePathAlbum.mkdirs())
								idAlbum = mDbHelper.insertAlbum(nameAlbum,
										pathAlbum, typeStorage);
						} else {
							if (!mDbHelper.checkExistsAlbum(nameAlbum)) {
								idAlbum = mDbHelper.insertAlbum(nameAlbum,
										pathAlbum, typeStorage);
							} else
								idAlbum = mDbHelper
										.getIdAlbumByNameAlbum(nameAlbum);
						}
						ArrayList<ModelMedia> arrMedias = null;
						if (activity.getString(R.string.all_media).equals(
								navigationSelected)) {
							arrMedias = mDbProvider.getAllMedias(album
									.getIdAlbum());
						} else if (activity.getString(R.string.photos).equals(
								navigationSelected)) {
							arrMedias = mDbProvider.getAllPhotos(album
									.getIdAlbum());
						} else {
							arrMedias = mDbProvider.getAllVideos(album
									.getIdAlbum());
						}
						if (arrMedias != null && arrMedias.size() > 0)
							hideMedia(activity, arrMedias, idAlbum, pathAlbum);
					}
				} else {
					if (arrMedias != null && arrMedias.size() > 0)
						hideMedia(activity, arrMedias, idAlbum, pathAlbum);
				}

			}
			return null;
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
			if (showProgress)
				progressDialog.setProgress(mProgressStatus);
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			if (showProgress)
				progressDialog.dismiss();
			mTask.onTaskComplete(arrMedias);
		}

		public void hideMedia(Activity activity,
				ArrayList<ModelMedia> arrMedias, long idAlbum, String pathAlbum) {
			for (ModelMedia media : arrMedias) {
				mProgressStatus++;
				try {
					/** Invokes the callback method onProgressUpdate */
					if (showProgress)
						publishProgress(mProgressStatus);
					/** Sleeps this thread for 100ms */
				} catch (Exception e) {

				}
				File fileIn = new File(media.getPathMediaStock());
				File fileOut;
				String pathMediaHidden = null;
				if (media.isVideo) {
					pathMediaHidden = pathAlbum + File.separator
							+ media.getNameMedia() + Config.EXETENSION_VIDEO;
					fileOut = new File(pathMediaHidden);
				} else {
					pathMediaHidden = pathAlbum + File.separator
							+ media.getNameMedia() + Config.EXETENSION;
					fileOut = new File(pathMediaHidden);
				}

				boolean result = fileIn.renameTo(fileOut);
				if (!result) {
					if (copyFile(fileIn.getAbsolutePath(),
							fileOut.getAbsolutePath())) {
						fileIn.delete();
					}
				}
				media.setIdAlbum(idAlbum);
				media.setPathMediaHidden(pathMediaHidden);
				if (media.isVideo) {
					media.setTypeMedia(Config.TYPE_VIDEO);
					mDbHelper.insertMedia(media);
					mDbProvider.deleteVideosProvider(media.getIdMedia());
				} else {
					media.setTypeMedia(Config.TYPE_PHOTO);
					mDbHelper.insertMedia(media);
					mDbProvider.deletePhotosProvider(media.getIdMedia());
				}
			}
			if (arrMedias != null && arrMedias.size() > 0) {
				Intent intent = new Intent(activity, HidePhotoService.class);
				intent.setAction(HidePhotoService.ACTION_GET_LIST_HIDDEN);
				intent.putParcelableArrayListExtra(Config.KEY_GET_LIST_HIDDEN,
						arrMedias);
				activity.startService(intent);
			}
		}

	}

	public static class HiddenMediaInAlbumView extends
			AsyncTask<Void, Integer, Void> {

		private Activity activity;

		private ArrayList<ModelMedia> arrMedias;

		private DbHelper mDbHelper;

		private DbProvider mDbProvider;

		private long idAlbum;

		private ProgressDialog progressDialog;

		private onTask mTask;

		private int mProgressStatus = 0;

		private int sumMedia = 0;

		private String nameAlbum;

		private String pathAlbum = null;

		private boolean showProgress;

		private int typeStorage;

		public HiddenMediaInAlbumView(Activity activity, Long idAlbum,
				String nameAlbum, ArrayList<ModelMedia> arrMedias,
				boolean showProgress, onTask mTask) {
			this.activity = activity;
			this.arrMedias = arrMedias;
			this.idAlbum = idAlbum;
			this.nameAlbum = nameAlbum;
			mDbHelper = DbHelper.getInstance(activity);
			mDbProvider = DbProvider.getinstance(activity);
			this.mTask = mTask;
			this.showProgress = showProgress;
			typeStorage = Integer.parseInt(StringPref.getPreference(activity,
					Config.KEY_STORAGE, "1"));
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pathAlbum = Config.getPathFinal(typeStorage) + nameAlbum;
			File filePathAlbum = new File(pathAlbum);
			if (!filePathAlbum.exists())
				filePathAlbum.mkdirs();
			if (idAlbum == -1 || idAlbum == 0) {
				if (mDbHelper.checkExistsAlbum(nameAlbum)) {
					idAlbum = mDbHelper.getIdAlbumByNameAlbum(nameAlbum);
				} else {
					idAlbum = mDbHelper.insertAlbum(nameAlbum, pathAlbum,
							typeStorage);
				}
			}
			if (showProgress) {
				progressDialog = new ProgressDialog(activity);
				sumMedia = arrMedias.size();
				progressDialog.setMax(sumMedia);
				progressDialog.setMessage(activity
						.getString(R.string.hiding_media));
				progressDialog
						.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
				progressDialog.setProgressDrawable(activity.getResources()
						.getDrawable(R.drawable.custom_progress));
				progressDialog.setCancelable(true);
				progressDialog.setProgress(0);
				progressDialog.show();
			}

		}

		@Override
		protected Void doInBackground(Void... params) {
			while (mProgressStatus < sumMedia) {
				if (nameAlbum != null && idAlbum != -1)
					hideMedia(activity, arrMedias, idAlbum, pathAlbum);
			}
			return null;
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
			if (showProgress)
				progressDialog.setProgress(mProgressStatus);
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			progressDialog.dismiss();
			if (arrMedias != null && arrMedias.size() > 0) {
				Intent intent = new Intent(activity, HidePhotoService.class);
				intent.setAction(HidePhotoService.ACTION_GET_LIST_HIDDEN);
				intent.putParcelableArrayListExtra(Config.KEY_GET_LIST_HIDDEN,
						arrMedias);
				activity.startService(intent);
			}
			mTask.onTaskComplete(arrMedias);
		}

		public void hideMedia(Activity activity,
				ArrayList<ModelMedia> arrMedias, long idAlbum, String pathAlbum) {
			for (ModelMedia media : arrMedias) {
				mProgressStatus++;
				try {
					/** Invokes the callback method onProgressUpdate */
					publishProgress(mProgressStatus);
					/** Sleeps this thread for 100ms */
				} catch (Exception e) {
				}

				File fileIn = new File(media.getPathMediaStock());
				File fileOut;
				String pathMediaHidden = null;
				if (media.isVideo) {
					pathMediaHidden = pathAlbum + File.separator
							+ media.getNameMedia() + Config.EXETENSION_VIDEO;
					fileOut = new File(pathMediaHidden);
				} else {
					pathMediaHidden = pathAlbum + File.separator
							+ media.getNameMedia() + Config.EXETENSION;
					fileOut = new File(pathMediaHidden);
				}

				try {
					boolean result = fileIn.renameTo(fileOut);
					if (!result) {
						if (copyFile(fileIn.getAbsolutePath(),
								fileOut.getAbsolutePath())) {
							fileIn.delete();
						}
					}
					media.setIdAlbum(idAlbum);
					media.setPathMediaHidden(pathMediaHidden);
					if (media.isVideo) {
						media.setTypeMedia(Config.TYPE_VIDEO);
						mDbProvider.deleteVideosProvider(media.getIdMedia());
						mDbHelper.insertMedia(media);
					} else {
						media.setTypeMedia(Config.TYPE_PHOTO);
						mDbProvider.deletePhotosProvider(media.getIdMedia());
						mDbHelper.insertMedia(media);
					}
				} catch (Exception e) {

				}
			}
		}

	}

	public static class UnHideMedia extends AsyncTask<Void, Integer, Void> {

		private Activity activity;

		private ArrayList<ModelAlbum> arrAlbums;

		private ArrayList<ModelMedia> arrMedias;

		private onTask mTask;

		private ProgressDialog progressDialog;

		private int sumMedia = 0;

		private String pathAlbumStock = null;

		private DbHelper mDbHelper;

		private DbProvider mDbProvider;

		private int mProgressStatus = 0;

		private String navigationSelected;

		private boolean showProgress;

		public UnHideMedia(Activity activity, ArrayList<ModelMedia> arrMedias,
				ArrayList<ModelAlbum> arrAlbums, String navigationSelected,
				boolean showProgress, onTask mTask) {
			this.mTask = mTask;
			this.activity = activity;
			this.arrAlbums = arrAlbums;
			this.arrMedias = arrMedias;
			mDbHelper = DbHelper.getInstance(activity);
			mDbProvider = DbProvider.getinstance(activity);
			this.navigationSelected = navigationSelected;
			this.showProgress = showProgress;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			if (arrAlbums != null) {
				if (navigationSelected.equals(activity
						.getString(R.string.all_media))) {
					for (ModelAlbum album : arrAlbums) {
						ArrayList<ModelMedia> arrMedias = mDbHelper
								.getAllMedia(album.getIdAlbum());
						sumMedia += arrMedias.size();
					}
				} else if (navigationSelected.equals(activity
						.getString(R.string.photos))) {
					for (ModelAlbum album : arrAlbums) {
						ArrayList<ModelMedia> arrMedias = mDbHelper
								.getAllPhotos(album.getIdAlbum());
						sumMedia += arrMedias.size();
					}
				} else {
					for (ModelAlbum album : arrAlbums) {
						ArrayList<ModelMedia> arrMedias = mDbHelper
								.getAllVideos(album.getIdAlbum());
						sumMedia += arrMedias.size();
					}
				}
			} else
				sumMedia = arrMedias.size();

			if (showProgress) {
				progressDialog = new ProgressDialog(activity);
				progressDialog.setMax(sumMedia);
				progressDialog.setMessage(activity
						.getString(R.string.hiding_media));
				progressDialog
						.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
				progressDialog.setProgressDrawable(activity.getResources()
						.getDrawable(R.drawable.custom_progress));
				progressDialog.setCancelable(true);
				progressDialog.setProgress(0);
				progressDialog.show();
			}

			// ========
			if (arrMedias != null && arrMedias.size() > 0) {
				String pathMediaStock = arrMedias.get(0).getPathMediaStock();
				String nameMediaStock = arrMedias.get(0).getNameMedia();
				try {
					if (pathMediaStock != null && nameMediaStock != null
							&& pathMediaStock.indexOf(nameMediaStock) != -1) {
						pathAlbumStock = pathMediaStock.replace(nameMediaStock,
								"");
					} else {
						pathAlbumStock = activity.getString(R.string.photos);
					}
					File fileAlbumStock = new File(pathAlbumStock);
					fileAlbumStock.mkdirs();
				} catch (NullPointerException e) {
					e.printStackTrace();
				}

			}

		}

		@Override
		protected Void doInBackground(Void... params) {
			if (arrAlbums != null && arrAlbums.size() > 0) {
				for (ModelAlbum album : arrAlbums) {
					if (activity.getString(R.string.all_media).equals(
							navigationSelected)) {
						arrMedias = mDbHelper.getAllMedia(album.getIdAlbum());
					} else if (activity.getString(R.string.photos).equals(
							navigationSelected)) {
						arrMedias = mDbHelper.getAllPhotos(album.getIdAlbum());
					} else {
						arrMedias = mDbHelper.getAllVideos(album.getIdAlbum());
					}
					if (arrMedias != null && arrMedias.size() > 0) {
						String pathMediaStock = arrMedias.get(0)
								.getPathMediaStock();
						String nameMediaStock = arrMedias.get(0).getNameMedia();
						if (pathMediaStock.indexOf(nameMediaStock) != -1) {
							pathAlbumStock = pathMediaStock.replace(
									nameMediaStock, "");
						} else {
							pathAlbumStock = activity
									.getString(R.string.photos);
						}
						File fileAlbumStock = new File(pathAlbumStock);
						fileAlbumStock.mkdirs();
					}
					if (arrMedias != null) {
						unhideMedia(activity, arrMedias, mTask);
						mDbHelper.deleteAlbumById(album.getIdAlbum());
						File filePathAlbum = new File(album.getPathAlbum());
						filePathAlbum.delete();
					}
				}
			} else {
				unhideMedia(activity, arrMedias, mTask);
			}

			return null;
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
			if (showProgress)
				progressDialog.setProgress(mProgressStatus);
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			if (showProgress)
				progressDialog.dismiss();
			mTask.onTaskComplete(arrMedias);
		}

		private void unhideMedia(Activity activity,
				ArrayList<ModelMedia> arrMedias, onTask mTask) {
			for (ModelMedia media : arrMedias) {
				mProgressStatus++;
				try {
					/** Invokes the callback method onProgressUpdate */
					if (showProgress)
						publishProgress(mProgressStatus);
					/** Sleeps this thread for 100ms */
				} catch (Exception e) {
				}

				File fileStock = new File(media.getPathMediaStock());
				File fileHidden = new File(media.getPathMediaHidden());
				File fileHiddenThumb = new File(media.getPathMediaHidden()
						+ Config.EXETENSION_THUMBNAIL);
				boolean result = fileHidden.renameTo(fileStock);
				if (!result) {
					if (copyFile(media.getPathMediaHidden(),
							media.getPathMediaStock())) {
						fileHidden.delete();
						fileHiddenThumb.delete();
						result = true;
					}
				} else {
					fileHiddenThumb.delete();
				}
				mDbHelper.deleteMediaById(media.getIdMedia());
				if (media.isVideo)
					mDbProvider.insertMedia(media.getNameMedia(),
							media.getPathMediaStock(), media.getDate(), true);
				else
					mDbProvider.insertMedia(media.getNameMedia(),
							media.getPathMediaStock(), media.getDate(), false);

			}

		}
	}

	public static class DeleteMediaStock extends AsyncTask<Void, Integer, Void> {

		private Activity activity;

		private ArrayList<ModelAlbum> arrAlbums;

		private ArrayList<ModelMedia> arrMedias;

		private onTask task;

		private DbProvider mDbProvider;

		private int mProgressStatus = 0;

		private ProgressDialog progressDialog;

		private int sumMedia = 0;

		private String navigationSelected;

		private boolean showProgress;

		public DeleteMediaStock(Activity activity,
				ArrayList<ModelMedia> arrMedias,
				ArrayList<ModelAlbum> arrAlbums, String navigationSelected,
				boolean showProgress, onTask task) {
			this.activity = activity;
			this.arrAlbums = arrAlbums;
			this.arrMedias = arrMedias;
			this.task = task;
			mDbProvider = DbProvider.getinstance(activity);
			this.navigationSelected = navigationSelected;
			this.showProgress = showProgress;

		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();

			if (arrAlbums != null) {
				if (navigationSelected.equals(activity
						.getString(R.string.all_media))) {
					for (ModelAlbum album : arrAlbums) {
						ArrayList<ModelMedia> arrMedias = mDbProvider
								.getAllMedias(album.getIdAlbum());
						sumMedia += arrMedias.size();
					}
				} else if (navigationSelected.equals(activity
						.getString(R.string.photos))) {
					for (ModelAlbum album : arrAlbums) {
						ArrayList<ModelMedia> arrMedias = mDbProvider
								.getAllPhotos(album.getIdAlbum());
						sumMedia += arrMedias.size();
					}
				} else {
					for (ModelAlbum album : arrAlbums) {
						ArrayList<ModelMedia> arrMedias = mDbProvider
								.getAllVideos(album.getIdAlbum());
						sumMedia += arrMedias.size();
					}
				}
			} else {
				sumMedia = arrMedias.size();
			}

			if (showProgress) {
				progressDialog = new ProgressDialog(activity);
				progressDialog.setMessage(activity
						.getString(R.string.deleting_media));
				progressDialog.setMax(sumMedia);
				progressDialog
						.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
				progressDialog.setProgressDrawable(activity.getResources()
						.getDrawable(R.drawable.custom_progress));
				progressDialog.setCancelable(true);
				progressDialog.setProgress(0);
				progressDialog.show();
			}

		}

		@Override
		protected Void doInBackground(Void... params) {
			if (arrAlbums != null && arrAlbums.size() > 0) {
				for (ModelAlbum album : arrAlbums) {
					ArrayList<ModelMedia> arrMedias = null;
					if (navigationSelected.equals(activity
							.getString(R.string.all_media))) {
						arrMedias = mDbProvider
								.getAllMedias(album.getIdAlbum());
					} else if (navigationSelected.equals(activity
							.getString(R.string.photos))) {
						arrMedias = mDbProvider
								.getAllPhotos(album.getIdAlbum());
					} else {
						arrMedias = mDbProvider
								.getAllVideos(album.getIdAlbum());
					}
					if (arrMedias != null)
						deleteMediaStock(arrMedias);
				}
			} else {
				deleteMediaStock(arrMedias);
			}

			return null;
		}

		private void deleteMediaStock(ArrayList<ModelMedia> arrMedias) {
			for (ModelMedia media : arrMedias) {
				mProgressStatus++;
				try {
					/** Invokes the callback method onProgressUpdate */
					if (showProgress)
						publishProgress(mProgressStatus);
					/** Sleeps this thread for 100ms */
				} catch (Exception e) {
				}
				File pathPhoto = new File(media.getPathMediaStock());
				pathPhoto.delete();
				if (media.isVideo())
					mDbProvider.deleteVideosProvider(media.getIdMedia());
				else
					mDbProvider.deletePhotosProvider(media.getIdMedia());

			}

		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
			if (showProgress)
				progressDialog.setProgress(mProgressStatus);
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			if (showProgress)
				progressDialog.dismiss();
			task.onTaskComplete(arrMedias);
		}

	}

	public static class DeleteMediaHidden extends
			AsyncTask<Void, Integer, Void> {

		private Activity activity;

		private ArrayList<ModelAlbum> arrAlbums;

		private ArrayList<ModelMedia> arrMedias;

		private onTask task;

		private DbHelper mDbHelper;;

		private int mProgressStatus = 0;

		private ProgressDialog progressDialog;

		private int sumMedia = 0;

		private String navigationSelected;

		private boolean showProgress;

		public DeleteMediaHidden(Activity activity,
				ArrayList<ModelMedia> arrMedias,
				ArrayList<ModelAlbum> arrAlbums, String navigationSelected,
				boolean showProgress, onTask task) {
			this.activity = activity;
			this.arrAlbums = arrAlbums;
			this.arrMedias = arrMedias;
			this.task = task;
			mDbHelper = DbHelper.getInstance(activity);
			this.navigationSelected = navigationSelected;
			this.showProgress = showProgress;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();

			if (arrAlbums != null) {
				if (navigationSelected.equals(activity
						.getString(R.string.all_media))) {
					for (ModelAlbum album : arrAlbums) {
						ArrayList<ModelMedia> arrMedias = mDbHelper
								.getAllMedia(album.getIdAlbum());
						sumMedia += arrMedias.size();
					}
				} else if (navigationSelected.equals(activity
						.getString(R.string.photos))) {
					for (ModelAlbum album : arrAlbums) {
						ArrayList<ModelMedia> arrMedias = mDbHelper
								.getAllPhotos(album.getIdAlbum());
						sumMedia += arrMedias.size();
					}
				} else {
					for (ModelAlbum album : arrAlbums) {
						ArrayList<ModelMedia> arrMedias = mDbHelper
								.getAllVideos(album.getIdAlbum());
						sumMedia += arrMedias.size();
					}
				}
			} else {
				sumMedia = arrMedias.size();
			}
			if (showProgress) {
				progressDialog = new ProgressDialog(activity);
				progressDialog.setMessage(activity
						.getString(R.string.deleting_media));
				progressDialog
						.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
				progressDialog.setProgressDrawable(activity.getResources()
						.getDrawable(R.drawable.custom_progress));
				progressDialog.setMax(sumMedia);
				progressDialog.setCancelable(true);
				progressDialog.setProgress(0);
				progressDialog.show();
			}

		}

		@Override
		protected Void doInBackground(Void... params) {
			if (arrAlbums != null && arrAlbums.size() > 0) {
				for (ModelAlbum album : arrAlbums) {
					ArrayList<ModelMedia> arrMedias = null;
					if (navigationSelected.equals(activity
							.getString(R.string.all_media))) {
						arrMedias = mDbHelper.getAllMedia(album.getIdAlbum());
					} else if (navigationSelected.equals(activity
							.getString(R.string.photos))) {
						arrMedias = mDbHelper.getAllPhotos(album.getIdAlbum());
					} else {
						arrMedias = mDbHelper.getAllVideos(album.getIdAlbum());
					}
					if (arrMedias != null) {
						deleteMediaStock(arrMedias);
						mDbHelper.deleteAlbumById(album.getIdAlbum());
						File filePathAlbum = new File(album.getPathAlbum());
						filePathAlbum.delete();
					}

				}
			} else {
				deleteMediaStock(arrMedias);
			}

			return null;
		}

		private void deleteMediaStock(ArrayList<ModelMedia> arrMedias) {
			for (ModelMedia media : arrMedias) {
				mProgressStatus++;
				try {
					/** Invokes the callback method onProgressUpdate */
					if (showProgress)
						publishProgress(mProgressStatus);
					/** Sleeps this thread for 100ms */
				} catch (Exception e) {
				}
				File pathPhoto = new File(media.getPathMediaHidden());
				pathPhoto.delete();
				mDbHelper.deleteMediaById(media.getIdMedia());
				File pathPhotoThumb = new File(media.getPathMediaHidden()
						+ Config.EXETENSION_THUMBNAIL);
				pathPhotoThumb.delete();
			}

		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
			if (showProgress)
				progressDialog.setProgress(mProgressStatus);
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			if (showProgress)
				progressDialog.dismiss();
			task.onTaskComplete(arrMedias);
		}

	}

	public static boolean checkIsImage(String path) {
		boolean result = false;
		if (path != null) {
			String ext = path.substring(path.lastIndexOf("."), path.length());
			if (ext.equalsIgnoreCase(".jpg") || ext.equalsIgnoreCase(".jpeg")
					|| ext.equalsIgnoreCase(".png")
					|| ext.equalsIgnoreCase(".bmp")
					|| ext.equalsIgnoreCase(".gif")
					|| ext.equalsIgnoreCase(".webp")) {
				result = true;
			}
		}
		return result;
	}

	public static boolean checkIsVideo(String path) {
		boolean result = false;
		if (path != null) {
			String ext = path.substring(path.lastIndexOf("."), path.length());
			if (ext.equalsIgnoreCase(".3gp") || ext.equalsIgnoreCase(".mp4")
					|| ext.equalsIgnoreCase(".webm")
					|| ext.equalsIgnoreCase(".mkv")
					|| ext.equalsIgnoreCase(".mov")) {
				result = true;
			}
		}
		return result;
	}

	public static void shareFile(Activity activity, Uri uri) {
		Intent shareIntent = new Intent();
		shareIntent.setAction(Intent.ACTION_SEND);
		shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
		shareIntent.setType("*/*");
		activity.startActivityForResult(Intent.createChooser(shareIntent,
				activity.getResources().getText(R.string.share)),
				Config.REQUEST_CODE_SHARE_FILE);
	}

	public static void shareFileMultiple(Activity activity,
			ArrayList<Uri> arrUri) {
		Intent shareIntent = new Intent();
		shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
		shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, arrUri);
		shareIntent.setType("*/*");
		activity.startActivityForResult(Intent.createChooser(shareIntent,
				activity.getResources().getText(R.string.share)),
				Config.REQUEST_CODE_SHARE_FILE);
	}

	public static class ShareFileStock extends AsyncTask<Void, Void, Void> {
		private Activity activity;
		private ArrayList<ModelAlbum> arrAlbums;
		private ArrayList<ModelMedia> arrMedias;
		private String navigationSelected;
		private DbProvider mDbProvider;
		private ArrayList<Uri> arrUris = new ArrayList<Uri>();
		private ProgressDialog progressDialog;

		public ShareFileStock(Activity activity,
				ArrayList<ModelAlbum> arrAlbums,
				ArrayList<ModelMedia> arrMedias, String navigationSelected) {
			this.activity = activity;
			this.arrAlbums = arrAlbums;
			this.arrMedias = arrMedias;
			this.navigationSelected = navigationSelected;
			mDbProvider = DbProvider.getinstance(activity);
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressDialog = ProgressDialog.show(activity, null,
					activity.getString(R.string.loading_photo));
		}

		@Override
		protected Void doInBackground(Void... params) {

			if (arrAlbums == null) {
				for (ModelMedia modelMedia : arrMedias) {
					File file = new File(modelMedia.getPathMediaStock());
					Uri uri = Uri.fromFile(file);
					arrUris.add(uri);
				}
			} else {
				for (ModelAlbum album : arrAlbums) {
					ArrayList<ModelMedia> arrMediasNew = null;
					if (activity.getString(R.string.all_media).equals(
							navigationSelected))
						arrMediasNew = mDbProvider.getAllMedias(album
								.getIdAlbum());
					else if (activity.getString(R.string.photos).equals(
							navigationSelected))
						arrMediasNew = mDbProvider.getAllPhotos(album
								.getIdAlbum());
					else if (activity.getString(R.string.videos).equals(
							navigationSelected))
						arrMediasNew = mDbProvider.getAllVideos(album
								.getIdAlbum());
					if (arrMediasNew != null && arrMediasNew.size() > 0) {
						for (ModelMedia modelMedia : arrMediasNew) {
							File file = new File(modelMedia.getPathMediaStock());
							Uri uri = Uri.fromFile(file);
							arrUris.add(uri);
						}
					}

				}

			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			progressDialog.dismiss();
			if (arrUris.size() == 1) {
				shareFile(activity, arrUris.get(0));
			} else {
				shareFileMultiple(activity, arrUris);
			}
		}
	}

	public static class ShareFileHidden extends AsyncTask<Void, Void, Void> {
		private Activity activity;
		private ArrayList<ModelAlbum> arrAlbums;
		private ArrayList<ModelMedia> arrMedias;
		private String navigationSelected;
		private DbHelper mDbHelper;
		private ArrayList<Uri> arrUris = new ArrayList<Uri>();
		private ProgressDialog progressDialog;

		private File dirCache = null;

		public ShareFileHidden(Activity activity,
				ArrayList<ModelAlbum> arrAlbums,
				ArrayList<ModelMedia> arrMedias, String navigationSelected) {
			this.activity = activity;
			this.arrAlbums = arrAlbums;
			this.arrMedias = arrMedias;
			this.navigationSelected = navigationSelected;
			mDbHelper = DbHelper.getInstance(activity);
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dirCache = activity.getExternalCacheDir();
			if (!dirCache.exists())
				dirCache.mkdirs();
			progressDialog = ProgressDialog.show(activity, null,
					activity.getString(R.string.loading_photo));
		}

		@Override
		protected Void doInBackground(Void... params) {
			if (arrAlbums == null) {
				for (ModelMedia media : arrMedias) {
					String outCache = dirCache.getAbsolutePath()
							+ File.separator + media.getNameMedia();
					String hiddenPath = media.getPathMediaHidden();
					if (Util.copyFile(hiddenPath, outCache)) {
						File file = new File(outCache);
						Uri uri = Uri.fromFile(file);
						arrUris.add(uri);
					}
				}
			} else {

				for (ModelAlbum album : arrAlbums) {
					ArrayList<ModelMedia> arrMediasNew = null;
					if (activity.getString(R.string.all_media).equals(
							navigationSelected))
						arrMediasNew = mDbHelper
								.getAllMedia(album.getIdAlbum());
					else if (activity.getString(R.string.photos).equals(
							navigationSelected))
						arrMediasNew = mDbHelper.getAllPhotos(album
								.getIdAlbum());
					else if (activity.getString(R.string.videos).equals(
							navigationSelected))
						arrMediasNew = mDbHelper.getAllVideos(album
								.getIdAlbum());
					if (arrMediasNew != null && arrMediasNew.size() > 0) {
						for (ModelMedia media : arrMediasNew) {
							String outCache = dirCache.getAbsolutePath()
									+ File.separator + media.getNameMedia();

							String hiddenPath = media.getPathMediaHidden();

							if (Util.copyFile(hiddenPath, outCache)) {
								File file = new File(outCache);
								Uri uri = Uri.fromFile(file);
								arrUris.add(uri);
							}
						}
					}
				}

			}

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			progressDialog.dismiss();
			if (arrUris.size() == 1) {
				shareFile(activity, arrUris.get(0));
			} else {
				shareFileMultiple(activity, arrUris);
			}
		}
	}

	public static class MoveMedia extends AsyncTask<Void, Integer, Void> {

		private DbHelper mDbHelper;

		private Activity activity;

		private ProgressDialog progressDialog;

		private int sumMedia;

		private int mProgressStatus = 0;

		private int type_storage;

		ArrayList<ModelAlbum> arrAlbums;

		public MoveMedia(Activity activity, int type_storage) {
			this.activity = activity;
			mDbHelper = DbHelper.getInstance(activity);
			this.type_storage = type_storage;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			// getAlbum whith old storage
			if (type_storage == Config.TYPE_STORAGE_INTERNAL)
				arrAlbums = mDbHelper.getAllAlbum(Config.TYPE_STORAGE_EXTERNAL);
			else
				arrAlbums = mDbHelper.getAllAlbum(Config.TYPE_STORAGE_INTERNAL);
			for (ModelAlbum album : arrAlbums) {
				ArrayList<ModelMedia> arrMedias = mDbHelper.getAllMedia(album
						.getIdAlbum());
				sumMedia += arrMedias.size();
			}
			progressDialog = new ProgressDialog(activity);
			progressDialog.setMax(sumMedia);
			progressDialog
					.setMessage(activity.getString(R.string.moving_media));
			progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			progressDialog.setProgressDrawable(activity.getResources()
					.getDrawable(R.drawable.custom_progress));
			progressDialog.setCancelable(true);
			progressDialog.setProgress(0);
			progressDialog.show();

		}

		@Override
		protected Void doInBackground(Void... params) {
			for (ModelAlbum album : arrAlbums) {
				ArrayList<ModelMedia> arrMedias = mDbHelper.getAllMedia(album
						.getIdAlbum());
				String newPathAlbum = Config.getPathFinal(type_storage)
						+ album.getNameAlbum() + File.separator;
				File fileNewPath = new File(newPathAlbum);
				if (!fileNewPath.exists())
					fileNewPath.mkdirs();
				for (ModelMedia media : arrMedias) {
					mProgressStatus++;
					try {
						/** Invokes the callback method onProgressUpdate */
						publishProgress(mProgressStatus);
						/** Sleeps this thread for 100ms */
					} catch (Exception e) {

					}
					String newPathMedia = null;
					if (media.isVideo()) {
						newPathMedia = newPathAlbum + media.getNameMedia()
								+ Config.EXETENSION_VIDEO;
					} else {
						newPathMedia = newPathAlbum + media.getNameMedia()
								+ Config.EXETENSION;
					}
					String ofdPathMedia = media.getPathMediaHidden();
					if (Util.copyFile(ofdPathMedia, newPathMedia)) {
						File fileOldMedia = new File(ofdPathMedia);
						fileOldMedia.delete();
						if (Util.copyFile(ofdPathMedia
								+ Config.EXETENSION_THUMBNAIL, newPathMedia
								+ Config.EXETENSION_THUMBNAIL)) {
							File fileOldMediaThumb = new File(ofdPathMedia
									+ Config.EXETENSION_THUMBNAIL);
							fileOldMediaThumb.delete();
						}
						media.setPathMediaHidden(newPathMedia);
						mDbHelper.updateMedia(media);
					}
				}
				mDbHelper.updateAlbum(album.getIdAlbum(), newPathAlbum,
						type_storage);
			}

			return null;
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
			progressDialog.setProgress(mProgressStatus);
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			progressDialog.dismiss();
		}

	}

}
